/**    
 * 文件名：ChangeRoomContractService.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年12月18日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.models.changeroom.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.LandeaConstants;
import com.logic.landseaserver.domain.ChangeRoomRecord;
import com.logic.landseaserver.domain.Contract;
import com.logic.landseaserver.persistence.read.ChangeRoomRecordReadMapper;
import com.logic.landseaserver.persistence.write.ChangeRoomRecordWriteMapper;
import com.logic.landseaserver.persistence.write.ContractWriteMapper;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：ChangeRoomContractService</br>
 * 类描述：为了防止循环引用</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年12月18日 下午2:15:42</br>
 * @version 1.0
 *
 */
@Service
public class ChangeRoomContractService
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ChangeRoomService.class);
    
    @Autowired
    private ChangeRoomRecordReadMapper changeRoomRecordReadMapper;
    
    @Autowired
    private ChangeRoomRecordWriteMapper changeRoomRecordWriteMapper;
    
    @Autowired
    private ContractWriteMapper contractWriteMapper;
    
    /**
     * 
     * [简要描述]：跨店换房签约B合同时处理</br>
     * [详细描述]：如果有跨店换房记录，需要更新B合同中的字段为A合同ID</br>
     * [作者]：Aaron(2017-12-18)</br>
     *
     * @param contractB
     *
     */
    public void processChangeRoomContract(Contract contractB)
    {
        Integer userId = contractB.getUserId();
        Integer projectId = contractB.getProjectId();
        
        LOGGER.info("processChangeRoomContract|开始处理跨店换房签约合同.userId=" + userId + ",projectId=" + projectId);
        
        // 查询用户+门店+跨店换房,且没有处理过的(状态为未处理或者null)
        ChangeRoomRecord changeRoomRecord = new ChangeRoomRecord();
        changeRoomRecord.setUserId(userId);
        changeRoomRecord.setDestProjectId(projectId);
        List<ChangeRoomRecord> changeRoomList = changeRoomRecordReadMapper.selectByUserProjectNotSame(changeRoomRecord);
        
        if (CollectionUtils.isEmpty(changeRoomList))
        {
            LOGGER.info("processChangeRoomContract|没有跨店换房记录，处理结束.");
            return;
        }
        
        // 可能存在在当前门店多条跨店申请
        if (changeRoomList.size() > 1)
        {
            LOGGER.info("processChangeRoomContract|userId=" + userId + "在,projectId=" + projectId + "存在"
                + changeRoomList.size() + "条跨店换房记录.");
        }
        
        // 获取第一条进行处理
        ChangeRoomRecord first = changeRoomList.get(0);
        LOGGER.info("processChangeRoomContract|获取第一条进行处理.ChangeRoomId=" + first.getId());
        
        // 获取A合同
        Integer contractIdA = first.getContractId();
        // 更新B合同
        Contract contractUpdateB = new Contract();
        contractUpdateB.setId(contractB.getId());
        contractUpdateB.setOriginalCId(contractIdA);
        contractUpdateB.setRentType(LandeaConstants.ReRentType.TURN);
        contractUpdateB.setSourceRentType(LandeaConstants.ReRentType.TURN);
        contractWriteMapper.updateByPrimaryKeySelective(contractUpdateB);
        
        // 更新换房记录
        ChangeRoomRecord updateChangeRoom = new ChangeRoomRecord();
        updateChangeRoom.setId(first.getId());
        updateChangeRoom.setDestRoomId(contractB.getRoomId());
        updateChangeRoom.editInitCommonField();
        changeRoomRecordWriteMapper.updateByPrimaryKeySelective(updateChangeRoom);
        
        LOGGER.info("processChangeRoomContract|更新B合同完毕.contractIdB=" + contractB.getId());
    }
}
